var apiHost = wx.getStorageSync('apiHost');   //调用接口请求地址
var yunId  = wx.getStorageSync('yunId');                //云id
var openId  = wx.getStorageSync('openId');              //openId
var deliveryAreaType = "";
Page({

  data: {
    toDoor: "开启",
    deliveryTimeType:"",
    deliveryAreaType:"",
    hiddenLoading: true,
    imgUrl : "",
    acceptType : "",
    isSelfClaim : ""
  },
  onLoad : function (options) {
      var that = this;
      wx.showToast({
          title:'加载中',
          icon:'loading',
          mask:true
      })
      that.shopShopDetail();//获取商品详情

      var acceptType = options.acceptType;//接单方式
      if(undefined!=acceptType){
          that.setData({
              acceptType:acceptType
          })
      }
  },
  //店铺状态开启按钮
  switch1Change: function (e) {
    // 判断是否打开
    if (e.detail.value) {
      this.setData({
          toDoor: "开启",
          isSelfClaim : "1"
      });
    } else {
      this.setData({
          toDoor: "关闭",
          isSelfClaim : "0"
      });
    }
  },
  //获取商品详情
  shopShopDetail:function(){
      var that = this;
      //获取店铺详情
      wx.request({
          url: apiHost+"xcx/store/detail",
          data: {
              yunId : yunId  //测试云店主id
          },
          header: {
              'content-type': 'application/json' // 默认值
          },
          success: function (res) {
            var code = res.data.code;
            var msg = res.data.msg

            if (code == "10000") {
              that.setData({
                detailList: res.data.data
              })
            }else{
              wx.showToast({
                title: msg,
                icon: 'none',
                duration: 2000,
                mask: true
              });
            }

          },
      });
  },
  //更新name
  changeData: function(deliveryTimeType){
      this.setData({
          deliveryTimeType: deliveryTimeType
      })
  },
  changeDeliveryAreaType: function (deliveryAreaType){
    this.setData({
      deliveryAreaType: deliveryAreaType
    })
  },
  //配送方式点击跳转
  distribution:function (e) {
    wx.showToast({
      title: '加载中',
      icon: 'loading',
      mask: true
    })
      var deliveryTimeType = e.target.dataset.toggle;
      wx.navigateTo({
        url: '../distribution/distribution?deliveryTimeType='+deliveryTimeType
      })
  },
  //接单方式
  acceptTypeGo : function (e) {
    wx.showToast({
      title: '加载中',
      icon: 'loading',
      mask: true
    })
      var acceptType = e.target.dataset.toggle;
      wx.navigateTo({
          url: '../acceptType/acceptType?acceptType='+acceptType
      })
  },
  //配送区域
  distributionArea:function(e){
    wx.showToast({
      title: '加载中',
      icon: 'loading',
      mask: true
    })
    // var acceptType = e.target.dataset.toggle;
    deliveryAreaType = e.currentTarget.dataset.deliveryareatype;
    wx.navigateTo({
      url: '../region/paisong?deliveryAreaType=' + deliveryAreaType
    })
  },
  //设置头像
  setPhotoInfo : function () {
      var that = this;
      wx.chooseImage({
          count: 1, // 默认9
          sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有
          sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
          success: function (res) {
              // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
              var tempFilePaths = res.tempFilePaths;
              that.upload(that, tempFilePaths);
              // upload(that, tempFilePaths);
          }
      })
  },
  //头像上传到外网
  upload : function (page, path) {
      wx.showToast({
          icon: "loading",
          title: "正在上传"
      }),
      wx.uploadFile({
          url: "http://file.jhjvip.cn:8888/image-server/uploadImage/oss",
          filePath: path[0],
          name: 'file',
          header: { "Content-Type": "multipart/form-data" },
          formData: {
              //和服务器约定的token, 一般也可以放在header中
              'session_token': wx.getStorageSync('session_token')
          },
          success: function (res) {

              if (res.statusCode != 200) {
                  wx.showModal({
                      title: '提示',
                      content: '上传失败',
                      showCancel: false
                  })
                  return;
              }
              var data = res.data
              var aa =  page.stringToJson(data);  //字符串转json

              var imgUrl = aa.data.url;

              page.setData({  //上传成功修改显示头像
                  imgUrl: imgUrl
              })
          },
          fail: function (e) {
              console.log(e);
              wx.showModal({
                  title: '提示',
                  content: '上传失败',
                  showCancel: false
              })
          },
          complete: function () {
              wx.hideToast();  //隐藏Toast
          }
      })
  },
  //表单提交
  formSubmit: function(e) {
      var that = this;
      var formData = e.detail.value;        //表单提交的整体json

      var name = formData.name;             //店铺名称
      var address = formData.address;       //店铺地址
      var phone = formData.phone;           //店铺电话
      var headImage = formData.headImage;   //店铺头像

      var information = "";

      if(name===""||name===null){
          information = "请填写店铺名称";

      }else if(address===""||address===null){
          information = "请填写店铺地址";
      }else if(phone===""||phone===null){
          information = "请填写店铺电话";
      }else if(headImage===""||headImage===null){
          information = "请上传店铺头像";
      }
      if(information!=""){
          that.setData({
              msg:information,
              showModal: true,
          });
      }


      if(that.data.showModal){
          return ;
      }


      that.etailShop(formData)
  },
  /**
   * 编辑店铺信息
   * @param formData
   */
  etailShop : function (formData) {

    var that=this;

      var name = formData.name;                         //店铺名称
      var address = formData.address;                   //店铺地址
      var phone = formData.phone;                       //店铺手机号
      var deliveryTimeType = formData.deliveryTimeType; //店铺
      var acceptType = formData.acceptType;             //店铺名称
      var deliveryAreaType = formData.deliveryAreaType; //店铺名称
      var isSelfClaim = formData.isSelfClaim;           //店铺名称
      var deliveryCost = formData.deliveryCost;         //店铺名称
      var deliveryFee = formData.deliveryFee;           //店铺名称
      var outCostFree = formData.outCostFree;           //店铺名称
      var headImage = formData.headImage;               //店铺名称

      wx.showLoading({
        title: '加载中',
        icon: 'loading',
      });

      wx.request({
        url: apiHost +"xcx/store/edit",
          data:{
              openId:openId,
              yunId : yunId,
              name : name,
              address : address,
              phone : phone,
              deliveryTimeType : deliveryTimeType,
              acceptType : acceptType,
              deliveryAreaType : deliveryAreaType,
              isSelfClaim : isSelfClaim,
              deliveryCost:deliveryCost,
              deliveryFee : deliveryFee,
              outCostFree : outCostFree,
              headImage:headImage,
          },
          header: {
              'content-type': 'application/json' // 默认值
          },
          success:function (e) {
            that.setData({
              msg: e.data.msg,
              showModal: true,
            });

            wx.hideLoading();   //隐藏加载loading

          }
      });
  },
  formReset: function() {
      console.log('form发生了reset事件');
      this.modalTap2();
  },
  /**
   *字符串转json
   */
  stringToJson:  function (data){
      return JSON.parse(data);
  },

  /**
   * 隐藏模态对话框
   */
  hideModal: function () {
      this.setData({
          showModal: false
      });
  },
  /**
   * 对话框确认按钮点击事件
   */
  onConfirm: function () {
      this.hideModal();
  },
});